function record_west_canteen() {
    var button = document.getElementById("choose-canteen");
    button.innerText = "河西食堂";
  }
  
  function record_east_canteen() {
    var button = document.getElementById("choose-canteen");
    button.innerText = "河东食堂";
  }
  
  function record_LiWa_canteen() {
    var button = document.getElementById("choose-canteen");
    button.innerText = "丽娃食堂";
  }

  function addFoodInfo() {
    var canteen = document.getElementById("choose-canteen");
    var canteenName = canteen.innerText;
    console.log(canteenName);
    if(canteenName=="选择食堂"){
      alert("请选择食堂");
      return;
    }
    var inputName = document.getElementById("addName");
    var foodName = inputName.value;
    console.log(foodName);
    if(canteenName==""){
      alert("请输入菜品名称");
      return;
    }
    var window = document.getElementById("input-window");
    var windowName = window.value;
    console.log(windowName);
    if(canteenName==""){
      alert("请输入窗口名称");
      return;
    }
    var price = document.getElementById("input-food-price");
    var priceValue = parseFloat(price.value);
    console.log(priceValue);
    if(canteenName==""){
      alert("请输入菜品价格");
      return;
    }
    axios({
      method: "GET",
      url:
        "http://124.71.207.55:8081/addDish/" +
        foodName +
        "/" +
        priceValue +
        "/" +
        canteenName +
        "/" +
        windowName,
    })
      .then((response) => {
        console.log(response);
        alert(response.data);
      })
      .catch((error) => {
        console.log(error);
        alert("菜品信息上传失败");
      });
  }

  function addWindow(){
    var windowName = $("#input-window").val();
    console.log(windowName);
    var canteenName = document.getElementById("choose-canteen").innerText;
    console.log(canteenName);
    if(!windowName){
      alert("请输入窗口名称");
      return
    }
    if(canteenName=="选择食堂"){
      alert("请选择食堂");
    }
    axios({
      method:"GET",
        url:"http://124.71.207.55:8081/addWindowsByCanteen/" +
        canteenName +
        "/" +
        windowName,
    }).then((response)=>{
      console.log(response);
      alert(response.data);
    }).catch((error)=>{
        console.log(error);
        alert("窗口添加失败");
    })
  }

  $("#addFoodPic").click(()=> {
    var foodName = document.getElementById("addName").value;
    var upload=document.querySelector("#addFormFile");
    if(!foodName){
      alert('请输入菜品名称');
          return;
    }
    if(!upload.files[0]){
      alert('请上传菜品图片');
          return;
    }
    const fileMaxSize = 1024;//1M
    var fileSize = upload.files[0].size;
    var size = fileSize / 1024;

    const isImg = ['image/jpg', 'image/png', 'image/jpeg','image/gif','image/bmp'].indexOf(upload.files[0].type) > -1;
    if(!isImg){
      console.log("请上传jpg/png/jpeg/gif/bmp文件");
      return;
    }

    const file = upload.files[0];
  if(size > fileMaxSize){
    // 讀取圖片文件
    //console.log(file);
    //alert("before:"+size);
    const reader = new FileReader();
    reader.readAsDataURL(file);

    reader.onload = function() {

      // 將DataURL轉換為圖片對象
      const img = new Image();
      img.src = reader.result;
      
      img.onload = function() {
        // 壓縮圖片
        const canvas = document.createElement('canvas');
        const ctx = canvas.getContext('2d');
        canvas.width = img.width / 2;
        canvas.height = img.height / 2;
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        canvas.toBlob(function(blob) {
          // 使用FormData對象上傳Blob對象到後端
          const formData = new FormData();
          formData.append("name", foodName);
          formData.append("photo", blob, 'compressed.jpg');
          //alert("after:"+blob.size/1024);
          axios.post("http://124.71.207.55:8081/uploadDishImgByName", formData)
          .then(function (response) {
            //console.log("图片上传成功", response);
            if (response.status == 200) {
              alert(response.data);
            }
          })
          .catch(function (error) {
            //console.log("图片上传失败", error);
            alert("菜品图片上传失败");
          });
          
        }, 'image/jpeg', 0.7);
      };
      
    };
  }else{
    const formData = new FormData();
    formData.append("name", foodName);
    formData.append("photo", upload.files[0]);
    axios.post("http://124.71.207.55:8081/uploadDishImgByName", formData)
          .then(function (response) {
            //console.log("图片上传成功", response);
            if (response.status == 200) {
              alert(response.data);
            }
          })
          .catch(function (error) {
            //console.log("图片上传失败", error);
            alert("菜品图片上传失败");
          });
  }
    
    /*
    var formData = new FormData();           
    formData.append("name", foodName);
    formData.append("photo", upload.files[0]);
    axios.post("http://124.71.207.55:8081/uploadDishImgByName", formData ).then(function (response) {
        console.log("文件上传成功", response);
        if (response.status == 200) {
          alert(response.data);
        }
      })
      .catch(function (error) {
        console.log("图片上传失败", error);
        alert("菜品图片上传失败");
      });*/
  })



  $("#addWindowPic").click(()=> {
       var windowName = document.getElementById("input-window").value
       var upload2=document.querySelector("#addFormFile2");
        if (!windowName) {
          alert('请输入窗口名称');
          return;
        }
        if (!upload2.files[0]) {
          alert('请上传窗口图片');
          return;
        }
        var fileMaxSize = 1024;//1M
        var fileSize = upload2.files[0].size;
        var size = fileSize / 1024;
        
        const isImg = ['image/jpg', 'image/png', 'image/jpeg','image/gif','image/bmp'].indexOf(upload2.files[0].type) > -1;
        if(!isImg){
          console.log("请上传jpg/png/jpeg/gif/bmp文件");
          return;
       }
       const file = upload2.files[0];
       if(size > fileMaxSize){
         // 讀取圖片文件
         //console.log(file);
         //alert("before:"+size);
         const reader = new FileReader();
         reader.readAsDataURL(file);
     
         reader.onload = function() {
     
           // 將DataURL轉換為圖片對象
           const img = new Image();
           img.src = reader.result;
           
           img.onload = function() {
             // 壓縮圖片
             const canvas = document.createElement('canvas');
             const ctx = canvas.getContext('2d');
             canvas.width = img.width / 2;
             canvas.height = img.height / 2;
             ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
             canvas.toBlob(function(blob) {
               // 使用FormData對象上傳Blob對象到後端
               const formData = new FormData();
               formData.append("name", windowName);
               formData.append("photo", blob, 'compressed.jpg');
               //alert("after:"+blob.size/1024);
               axios.post("http://124.71.207.55:8081/uploadWindowsImgByName", formData)
               .then(function (response) {
                 //console.log("图片上传成功", response);
                 if (response.status == 200) {
                   alert(response.data);
                 }
               })
               .catch(function (error) {
                 //console.log("图片上传失败", error);
                 alert("菜品图片上传失败");
               });
               
             }, 'image/jpeg', 0.7);
           };
           
         };
       }else{
         const formData = new FormData();
         formData.append("name", windowName);
        formData.append("photo", upload2.files[0]);
         axios.post("http://124.71.207.55:8081/uploadWindowsImgByName", formData)
               .then(function (response) {
                 //console.log("图片上传成功", response);
                 if (response.status == 200) {
                   alert(response.data);
                 }
               })
               .catch(function (error) {
                 //console.log("图片上传失败", error);
                 alert("菜品图片上传失败");
               });
       }
/*
        var formData = new FormData();           
        formData.append("name", windowName);
        formData.append("photo", upload2.files[0]);
        axios.post("http://124.71.207.55:8081/uploadWindowsImgByName", formData)
            .then(function (response) {
              console.log("文件上传成功", response);
              if (response.status == 200) {
                alert(response.data);
              }
            })
            .catch(function (error) {
              console.log("图片上传失败", error);
              alert("窗口图片上传失败");
            });*/
  })


  $("#logout").click(function () {
    localStorage.setItem("name", "");
    localStorage.setItem("password", "");
    location.href=("login.html");
})



